package migrate

import (
	"system-admin/config/mysql"
	"system-admin/internal/app/web/service/alarm"
	"system-admin/internal/app/web/service/auth"
	"system-admin/internal/models"
	v2 "system-admin/internal/models/history/v2"

	"gorm.io/gorm"
)

type MigrateFunc struct{}

// Script3
// 删除权限
func (MigrateFunc) Script3() error {
	db := mysql.NewDB()
	// 删除索引
	db.Migrator().DropIndex(&v2.AuthRule{}, "name")
	db.Migrator().DropIndex(&v2.AuthRule{}, "unique_value")
	db.Migrator().DropIndex(&v2.AuthRule{}, "idx_unique_name")
	// 表结构迁移
	mysql.NewDB().
		Set("gorm:table_options", "ENGINE=InnoDB").
		Set("gorm:table_options", "CHARSET=utf8mb4").
		Set("gorm:table_options", "COLLATE=utf8mb4_general_ci").
		AutoMigrate(&v2.AuthRule{})
	// 删除地图管理路由
	auth.DeleteAuthRule("device-admin/map")
	// 删除原远程监控下的操作权限[读写权限]
	auth.DeleteAuthRule("device-admin/remote-monitor/edit")
	auth.DeleteAuthRule("device-admin/remote-monitor/read")
	return nil
}

// Script4
// 优化
func (MigrateFunc) Script4() error {
	db := mysql.NewDB()
	db.Migrator().AutoMigrate(&models.AlarmNotifyRecord{})
	return nil
}

func (MigrateFunc) Script202401() error {
	db := mysql.NewDB()
	tx := db.Set("gorm:table_options", "ENGINE=InnoDB").
		Set("gorm:table_options", "CHARSET=utf8mb4").
		Set("gorm:table_options", "COLLATE=utf8mb4_general_ci").
		Session(&gorm.Session{})
	tx.Migrator().DropIndex(&models.AlarmNotifyRecord{}, "idx_robot_id")            // 去除索引，使用联合索引
	tx.Migrator().DropIndex(&models.AlarmNotifyRecord{}, "idx_created_at")          // 去除索引，使用联合索引
	tx.Migrator().CreateIndex(&models.AlarmNotifyRecord{}, "idx_createdAt_robotID") // 增加联合索引
	// 修改robot_id的编码为utf8mb4_general_ci
	tx.Exec("ALTER TABLE `alarm_notify_record` MODIFY COLUMN `robot_id` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '设备id'")
	// 通知记录增加推送结果
	tx.Migrator().AddColumn(&models.AlarmNotifyRecord{}, "push_result")
	// 删除零售弃用的权限
	auth.DeleteAuthRule("sale-admin/device")                              // 零售后台设备管理
	auth.DeleteAuthRule("sale-admin/goods/goodsList/read")                // 商品列表-读权限
	auth.DeleteAuthRule("sale-admin/goods/goodsList/edit")                // 商品列表-更改权限
	auth.DeleteAuthRule("sale-admin/goods/classList/read")                // 商品列表-读权限
	auth.DeleteAuthRule("sale-admin/goods/classList/edit")                // 商品列表-更改权限
	auth.DeleteAuthRule("sale-admin/goods/goodsStock/read")               // 商品补货-更改权限
	auth.DeleteAuthRule("sale-admin/goods/goodsStock/edit")               // 商品补货-更改权限
	auth.DeleteAuthRule("sale-admin/goods/goodsStock")                    // 商品补货-更改权限
	auth.DeleteAuthRule("sale-admin/order/orderList/read")                // 订单-订单列表-读权限
	auth.DeleteAuthRule("sale-admin/order/orderList/edit")                // 订单-订单列表-更改权限
	auth.DeleteAuthRule("sale-admin/order/financeList")                   // 订单-收入流水(旧)
	auth.DeleteAuthRule("sale-admin/statistics/coreData")                 // 数据统计-核心看板
	auth.DeleteAuthRule("sale-admin/statistics/realTimeData")             // 数据统计-实时数据
	auth.DeleteAuthRule("sale-admin/activity/read")                       // 留资活动-读权限
	auth.DeleteAuthRule("sale-admin/activity/edit")                       // 留资活动-更改权限
	auth.DeleteAuthRule("sale-admin/activity")                            // 留资活动
	auth.DeleteAuthRule("sale-admin/cdkey/cdkeyList/read")                // 兑换码-兑换码列表-读权限
	auth.DeleteAuthRule("sale-admin/cdkey/cdkeyList/edit")                // 兑换码-兑换码列表-更改权限
	auth.DeleteAuthRule("sale-admin/cdkey/cdkeyList")                     // 兑换码-兑换码列表
	auth.DeleteAuthRule("sale-admin/cdkey/invalidList")                   // 兑换码-失效兑换码
	auth.DeleteAuthRule("sale-admin/cdkey/recordList/read")               // 兑换码-生成记录-读权限
	auth.DeleteAuthRule("sale-admin/cdkey/recordList/edit")               // 兑换码-生成记录-更改权限
	auth.DeleteAuthRule("sale-admin/cdkey/recordList")                    // 兑换码-生成记录
	auth.DeleteAuthRule("sale-admin/cdkey")                               // 兑换码
	auth.DeleteAuthRule("sale-admin/advert/advertMaterial/read")          // 广告-素材-读权限
	auth.DeleteAuthRule("sale-admin/advert/advertMaterial/edit")          // 广告-素材-更改权限
	auth.DeleteAuthRule("sale-admin/advert/advertMaterial")               // 广告-素材
	auth.DeleteAuthRule("sale-admin/advert/advertList")                   // 广告列表
	auth.DeleteAuthRule("sale-admin/advert/advertReview/read")            // 广告-审核-更改权限
	auth.DeleteAuthRule("sale-admin/advert/advertReview/edit")            // 广告-审核-更改权限
	auth.DeleteAuthRule("sale-admin/advert/advertReview")                 // 广告审核
	auth.DeleteAuthRule("sale-admin/advert")                              // 广告
	auth.DeleteAuthRule("sale-admin/merchant/read")                       // 商户管理-读权限
	auth.DeleteAuthRule("sale-admin/merchant/edit")                       // 商户管理-更改权限
	auth.DeleteAuthRule("sale-admin/system-setting/payment-setting/read") // 系统设置-支付设置-读权限
	auth.DeleteAuthRule("sale-admin/system-setting/payment-setting/edit") // 系统设置-支付设置-更改权限
	// 删除设备后台弃用的权限
	auth.DeleteAuthRule("device-admin/home")                    // 仪表盘
	auth.DeleteAuthRule("device-admin/device/device-list/read") // 设备-设备列表，读权限
	auth.DeleteAuthRule("device-admin/device/device-list/edit") // 设备-设备列表，更改权限
	auth.DeleteAuthRule("device-admin/device/location/read")    // 设备-位置列表，读权限
	auth.DeleteAuthRule("device-admin/device/location/edit")    // 设备-位置列表，更改权限
	auth.DeleteAuthRule("device-admin/remote-control/read")     // 远程维护，更改权限
	auth.DeleteAuthRule("device-admin/remote-control/edit")     // 远程维护，更改权限
	auth.DeleteAuthRule("device-admin/alarm-setting/read")      // 告警设置，读权限
	auth.DeleteAuthRule("device-admin/alarm-setting/edit")      // 告警设置，更改权限

	// 删除系统后台弃用的权限
	auth.DeleteAuthRule("system-admin/merchant/read")                   // 商户管理-读权限
	auth.DeleteAuthRule("system-admin/merchant/edit")                   // 商户管理-更改权限
	auth.DeleteAuthRule("system-admin/user/read")                       // 用户管理-读权限
	auth.DeleteAuthRule("system-admin/user/edit")                       // 用户管理-更改权限
	auth.DeleteAuthRule("system-admin/role/read")                       // 角色管理-读权限
	auth.DeleteAuthRule("system-admin/role/edit")                       // 角色管理-更改权限
	auth.DeleteAuthRule("system-admin/alarm/alarm-setting/read")        // 告警设置-读权限
	auth.DeleteAuthRule("system-admin/alarm/alarm-setting/edit")        // 告警设置-更改权限
	auth.DeleteAuthRule("system-admin/alarm/alarm-setting")             // 告警设置
	auth.DeleteAuthRule("system-admin/alarm/alarm-notify-setting/read") // 告警通知设置-读权限
	auth.DeleteAuthRule("system-admin/alarm/alarm-notify-setting/edit") // 告警通知设置-更改权限
	auth.DeleteAuthRule("system-admin/alarm/alarm-record/read")         // 告警记录-读权限
	auth.DeleteAuthRule("system-admin/alarm/alarm-record/edit")         // 告警记录-更改权限

	// 删除互动平台弃用的权限
	auth.DeleteAuthRule("interactive-admin/member/member-list") // 会员管理-会员列表
	auth.DeleteAuthRule("interactive-admin/member")             // 会员管理
	return nil
}

// 变更告警类型
func (MigrateFunc) Script202403() error {
	tx := mysql.NewDB().
		Set("gorm:table_options", "ENGINE=InnoDB").
		Set("gorm:table_options", "CHARSET=utf8mb4").
		Set("gorm:table_options", "COLLATE=utf8mb4_general_ci").
		Session(&gorm.Session{})
	// 更新角色规则索引约束
	tx.Migrator().DropIndex(&models.RoleAuthRule{}, "role_rule")
	tx.Migrator().AutoMigrate(&models.RoleAuthRule{})
	// 更新告警类型
	alarm.FixAlarmType()
	alarm.FixNotifyRecords()
	// 自动更新远程维护子权限
	auth.AddRemoteMonitorAuth()
	// 更新权限
	auth.UpdateAuthRules(mysql.NewDB(), auth.AuthRulesDefined, "-1")
	return nil
}

func (MigrateFunc) Script202404() error {
	// 更新权限
	auth.UpdateAuthRules(mysql.NewDB(), auth.AuthRulesDefined, "-1")
	return nil
}

func (MigrateFunc) Script202405() error {
	// 删除旧的设备状态菜单
	auth.DeleteAuthRule("device-admin/device/device-status")
	return nil
}

func (MigrateFunc) Script202406() error {
	// 删除旧的身份定制权限
	auth.DeleteAuthRule("market-support/material/theme")
	auth.DeleteAuthRule("market-support/material/voice")
	auth.DeleteAuthRule("market-support/material/emoji")
	auth.DeleteAuthRule("market-support/material/light")
	auth.DeleteAuthRule("market-support/material/material-audit")
	auth.DeleteAuthRule("market-support/material/material-audit-log")
	auth.DeleteAuthRule("market-support/material")
	return nil
}
